package myJs

import myJs.Utils.{g, _}
import myJs.implicits.Implicits._
import myJs.myPkg.Implicits._
import myJs.myPkg.jquery._
import myJs.myPkg.{ColumnOptions, ExportOptions, Select2Options, TableOptions}
import org.scalajs.dom.ext.Ajax

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}


/**
 * Created by yz on 11/5/2020
 */
@JSExportTopLevel("Statistic")
object Statistic {

  val tableId = "table"
  val bsDivId = "bs"
  val gtexDivId = "gtex"

  @JSExport("init")
  def init = {
    initTable
    refreshTable

  }

  def refreshTable = {
    refreshLsFsTable
    refreshBsTable
    refreshGtexTable
  }

  def refreshLsFsTable = {
    val url = g.jsRoutes.controllers.StatisticController.getLsFsStatData().url.toString
    Ajax.get(url = s"${url}").map { xhr =>
      val data = xhr.responseText.toJArJDS
      $(s"#table").bootstrapTable("load", data)
    }
  }

  def refreshBsTable = {
    val fileName = "bs.csv"
    val url = g.jsRoutes.controllers.StatisticController.getStatData().url.toString
    Ajax.get(url = s"${url}?fileName=${fileName}").map { xhr =>
      val data = xhr.responseText.toJArJDS
      $(s"#${bsDivId} #table").bootstrapTable("load", data)
    }
  }

  def refreshGtexTable = {
    val fileName = "GTEx_number_count.csv"
    val url = g.jsRoutes.controllers.StatisticController.getStatData().url.toString
    Ajax.get(url = s"${url}?fileName=${fileName}").map { xhr =>
      val data = xhr.responseText.toJArJDS
      $(s"#${gtexDivId} #table").bootstrapTable("load", data)
    }
  }

  def tbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => columnName match {
    case _ => v
  }

  def footerFmt(columnName: String): js.Function = (rows: js.Array[js.Dictionary[js.Any]]) => columnName match {
    case "Cancer_type" => "Total"
    case "Cancer_full name" => "-"
    case x =>
      val sum = rows.map(_ (x).toString.toDouble).sum
      sum
  }

  def gtexFooterFmt(columnName: String): js.Function = (rows: js.Array[js.Dictionary[js.Any]]) => columnName match {
    case "Tissue" => "Total"
    case x =>
      val sum = rows.map(_ (x).toString.toDouble).sum
      sum
  }

  def initLsFsTable = {
    val columnNames = List("Cancer_type", "Cancer_full name", "Sample_NO.", "Cancer_NO.", "NT_NO.",
      "Linear_Junction.NO.", "Fusion_Junction.NO.", "Annotation_Junction.NO.", "Unannotated_Junction.NO.")
    val columns = columnNames.map { columnName =>
      val title = columnName
      val fmt = tbFmt(columnName)
      val ftFmt = footerFmt(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).formatter(fmt).footerFormatter(ftFmt)
      columnName match {
        case _ => basicOptions
      }
    }
    val exportOptions = ExportOptions.fileName("statistics.1")
    val options = TableOptions.columns(columns.toJSArray).showFooter(true).height(600).pagination(false).
      exportOptions(exportOptions).exportTypes(js.Array("csv")).showExport(true)
    $(s"#${tableId}").bootstrapTable(options)
    bindEvt
  }

  def initBsTable = {
    val divId = "bs"
    val columnNames = List("Cancer_type", "Cancer_full name", "Sample_NO.", "BS_Junction.NO.")
    val columns = columnNames.map { columnName =>
      val title = columnName
      val fmt = tbFmt(columnName)
      val ftFmt = footerFmt(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).formatter(fmt).footerFormatter(ftFmt)
      columnName match {
        case _ => basicOptions
      }
    }
    val exportOptions = ExportOptions.fileName("statistics.2")
    val options = TableOptions.columns(columns.toJSArray).showFooter(true).height(600).pagination(false).
      exportOptions(exportOptions).exportTypes(js.Array("csv")).showExport(true)
    $(s"#${divId} #${tableId}").bootstrapTable(options)
    bindEvt
  }

  def initTable = {
    initLsFsTable
    initBsTable
    initGtexTable
  }

  def initGtexTable = {
    val divId = gtexDivId
    val columnNames = List("Tissue", "Sample_NO.", "Linear_Junction.NO.")
    val columns = columnNames.map { columnName =>
      val title = columnName
      val fmt = tbFmt(columnName)
      val ftFmt = gtexFooterFmt(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).formatter(fmt).footerFormatter(ftFmt)
      columnName match {
        case _ => basicOptions
      }
    }
    val exportOptions = ExportOptions.fileName("GTEx_number_count")
    val options = TableOptions.columns(columns.toJSArray).showFooter(true).height(600).pagination(false).
      exportOptions(exportOptions).exportTypes(js.Array("csv")).showExport(true)
    $(s"#${divId} #${tableId}").bootstrapTable(options)
    bindEvt
  }


  def bindEvt = {

  }


}
